import request, { request as requestMethods } from '@/utils/request';

/**
 * 获取登录验证码
 * @param {Object|string} data - 手机号或包含手机号的对象
 */
export const getLoginCode = (data) => {
    // 检查参数是否为字符串（直接传入邮箱）
    const email = typeof data === 'string' ? data : data?.email || data?.account;
    
    if (!email) {
        console.error('获取登录验证码缺少邮箱参数');
        return Promise.reject(new Error('请提供邮箱'));
    }

    console.log('获取登录验证码，邮箱:', email);
    
    return requestMethods.post('/email/send', {
        email,
        type: 'login'
    }, false)
    .then(response => {
        console.log('获取验证码成功:', response);
        return response;
    })
    .catch(error => {
        console.error('获取验证码失败:', error);
        throw error;
    });
};

/**
 * 登录
 * @param {Object} data - 登录信息
 */
export const login = (data) => {
    // 验证必填字段
    if (!data.account) {
        return Promise.reject(new Error('请输入账号'));
    }

    // 根据登录类型验证必填字段
    if (data.loginType === 'password' && !data.password) {
        return Promise.reject(new Error('请输入密码'));
    } else if (data.loginType === 'code' && !data.code) {
        return Promise.reject(new Error('请输入验证码'));
    }

    // 构建请求数据
    const requestData = {
        account: data.account,
        loginType: data.loginType
    };

    // 根据登录类型添加相应字段
    if (data.loginType === 'password') {
        requestData.password = data.password;
    } else if (data.loginType === 'code') {
        requestData.code = data.code;
    }

    console.log('登录请求参数:', JSON.stringify(requestData));
    
    return requestMethods.post('/user/login', requestData, false)
        .then(response => {
            console.log('登录返回结果:', JSON.stringify(response));
            return response;
        })
        .catch(error => {
            console.error('登录错误:', error);
            throw error;
        });
};

/**
 * 验证码注册
 * @param {Object} data - 注册信息
 */
export const registerByCode = (data) => {
    console.log('验证码注册请求参数:', JSON.stringify(data));
    
    return requestMethods.post('/user/register', {
        account: data.account,
        password: data.password,
        code: data.code
    }, false)
    .then(response => {
        console.log('验证码注册返回结果:', JSON.stringify(response));
        return response;
    })
    .catch(error => {
        console.error('验证码注册错误:', error);
        throw error;
    });
};

/**
 * 账号密码注册
 * @param {Object} data - 注册信息
 */
export const registerByAccount = (data) => {
    console.log('账号密码注册请求参数:', JSON.stringify(data));
    
    return requestMethods.post('/account/register', {
        username: data.username,
        nickname: data.nickname,
        mobile: data.mobile,
        email: data.email,
        password: data.password
    }, false)
    .then(response => {
        console.log('账号密码注册返回结果:', JSON.stringify(response));
        return response;
    })
    .catch(error => {
        console.error('账号密码注册错误:', error);
        throw error;
    });
};

/**
 * 获取注册验证码
 * @param {Object|string} data - 手机号/邮箱信息
 */
export const getRegisterCode = (data) => {
    const email = typeof data === 'string' ? data : data?.email || data?.account;
    
    if (!email) {
        console.error('获取注册验证码缺少邮箱参数');
        return Promise.reject(new Error('请提供邮箱'));
    }
    
    return requestMethods.post('/email/send', {
        email,
        type: 'register'
    }, false);
};

/**
 * 获取重置密码验证码
 * @param {Object|string} data - 手机号/邮箱
 */
export const getResetPasswordCode = (data) => {
    const email = typeof data === 'string' ? data : data?.email || data?.account;
    
    if (!email) {
        console.error('获取重置密码验证码缺少邮箱参数');
        return Promise.reject(new Error('请提供邮箱'));
    }
    
    return requestMethods.post('/user/reset-password/email', {
        email
    }, false);
};

/**
 * 重置密码
 * @param {Object} data - 重置密码信息
 */
export const resetPassword = (data) => {
    return requestMethods.post('/user/password/reset', data, false);
};

/**
 * 退出登录
 */
export const logout = async () => {
    try {
        await requestMethods.post('/user/logout');
        return true;
    } catch (error) {
        console.error('退出登录失败:', error);
        return false;
    }
};

/**
 * 刷新token
 */
export const refreshToken = () => {
    return requestMethods.post('/user/refresh-token');
};

/**
 * 获取当前用户信息
 */
export const getUserInfo = () => {
    return requestMethods.get('/user/info');
};

/**
 * 更新用户信息
 * @param {Object} userInfo - 用户信息
 */
export const updateUserInfo = (userInfo) => {
    return requestMethods.put('/user/info', userInfo);
};

/**
 * 修改密码
 * @param {Object} data - 密码信息
 */
export const changePassword = (data) => {
    return requestMethods.post('/user/password/change', data);
};

export default {
    getLoginCode,
    login,
    registerByCode,
    registerByAccount,
    getRegisterCode,
    getResetPasswordCode,
    resetPassword,
    logout,
    refreshToken,
    getUserInfo,
    updateUserInfo,
    changePassword
}; 